package com.iman.common.util;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

//这个类暂时用不到，先不用填写方法。
public class FreeMarkerUtils {
    static final Configuration cfg = new Configuration();

    public static Template getTemplate(String path, String templateId) {
        try {
            cfg.setDirectoryForTemplateLoading(new File(FreeMarkerUtils.class.getClassLoader().getResource(path).getPath()));
            return cfg.getTemplate(templateId + ".ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    };

    @SuppressWarnings("unchecked")
    public static String getTemplateContent(String path, String templateId, Map paramMap) {
        StringWriter writer = new StringWriter();
        try {
            cfg.setDirectoryForTemplateLoading(new File(FreeMarkerUtils.class.getClassLoader().getResource(path).getPath()));
            cfg.setDefaultEncoding("UTF-8");
            Template template = cfg.getTemplate(templateId + ".ftl");
            template.process(paramMap, writer);
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "free marker template exception occured!";
    };

//    public static String getTemplateContent(String templateId, Map paramMap) {
//        StringWriter writer = new StringWriter();
//        try {
//            String path = new File(FreeMarkerUtils.class.getResource("FreeMarkerUtils.class").getPath()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getPath();
//            Configuration cfg = new Configuration();
//            cfg.setDirectoryForTemplateLoading(new File(path + "/ftl"));
//            cfg.setDefaultEncoding("UTF-8");
//            Template template = cfg.getTemplate(templateId + ".ftl");
//            template.process(paramMap, writer);
//            return writer.toString();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                writer.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return "free marker template exception occured!";
//    };

    /******add by  xiaolei1.wang ,because  file Io  is  keep going *********/
//    public static String getTemplateContentCache(String templateId, Map paramMap) {
//        StringWriter writer = new StringWriter();
//        try {
//            Template template = cacheTemplage.get(templateId);
//            if (template == null) {
//                String path = new File(FreeMarkerUtils.class.getResource("FreeMarkerUtils.class").getPath()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getPath();
//                Configuration cfg = new Configuration();
//                cfg.setDirectoryForTemplateLoading(new File(path + "/ftl"));
//                cfg.setDefaultEncoding("UTF-8");
//                template = cfg.getTemplate(templateId + ".ftl");
//            }
//            template.process(paramMap, writer);
//            return writer.toString();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                writer.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return "free marker template exception occured!";
//    };

    // ===========copy from jeecms FrontUtils==================//
    // public static void includePagination(CmsSite site,
    // Map<String, TemplateModel> params, Environment env)
    // throws TemplateException, IOException {
    // String sysPage = DirectiveUtils.getString(PARAM_SYS_PAGE, params);
    // String userPage = DirectiveUtils.getString(PARAM_USER_PAGE, params);
    // if (!StringUtils.isBlank(sysPage)) {
    // String tpl = TPL_STYLE_PAGE_CHANNEL + sysPage + TPL_SUFFIX;
    // env.include(tpl, UTF8, true);
    // } else if (!StringUtils.isBlank(userPage)) {
    // String tpl = getTplPath(site.getSolutionPath(), TPLDIR_STYLE_LIST,
    // userPage);
    // env.include(tpl, UTF8, true);
    // } else {
    // // 没有包含分页
    // }
    // }

    /**
     * 标签中包含页面
     */
    // public static void includeTpl(String tplName, CmsSite site,
    // Map<String, TemplateModel> params, Environment env)
    // throws IOException, TemplateException {
    // String subTpl = DirectiveUtils.getString(PARAM_TPL_SUB, params);
    // String tpl;
    // if (StringUtils.isBlank(subTpl)) {
    // tpl = getTplPath(site.getSolutionPath(), TPLDIR_TAG, tplName);
    // } else {
    // tpl = getTplPath(site.getSolutionPath(), TPLDIR_TAG, tplName + "_"
    // + subTpl);
    // }
    // env.include(tpl, UTF8, true);
    // }

    /**
     * 标签中包含用户预定义列表样式模板
     */
    // public static void includeTpl(String listStyle, CmsSite site,
    // Environment env) throws IOException, TemplateException {
    // String tpl = getTplPath(site.getSolutionPath(), TPLDIR_STYLE_LIST,
    // listStyle);
    // env.include(tpl, UTF8, true);
    // }

    public static void main(String... sfd) throws IOException {
        System.out.println(new File("../../../ftl").getCanonicalPath());
    }
}
